using System;
using System.Data;
using System.Windows.Forms;

namespace Glpt.Common
{
    /// <summary>
    /// 数据绑定类
    /// </summary>
    public class DataDictionary
    {
        /// <summary>
        /// 数据字典表
        /// </summary>
        public static DataTable DictTable = null;

        /// <summary>
        /// 获取系统参数值
        /// </summary>
        /// <param name="dict_code">字典编号</param>
        /// <returns>参数值</returns>
        public static string GetDictParamValue(int dict_code)
        {
            return GetDictParamValue(dict_code, DictTable);
        }

        /// <summary>
        /// 获取系统参数值
        /// </summary>
        /// <param name="dict_code">字典编号</param>
        /// <param name="dt">数据表</param>
        /// <returns>参数值</returns>
        public static string GetDictParamValue(int dict_code, DataTable dt)
        {
            DataRow[] drs = dt.Select("DICT_CODE=" + dict_code.ToString());
            if (drs.Length == 0)
                throw new Exception("没有找到该编号的系统参数");
            else
                return drs[0]["DICT_VALUE"].ToString().Trim();
        }

        /// <summary>
        /// 获取字典表
        /// </summary>
        /// <param name="dict_code">字典编号</param>
        /// <returns>表数据</returns>
        public static DataTable GetDictTable(int dict_code)
        {
            if (DictTable == null)
                throw new Exception("数据字典列表没有缓存");
            DataTable dt = DictTable.Clone();
            dt.CaseSensitive = true;
            DataRow[] drs = DictTable.Select("DICT_CODE=" + dict_code.ToString());
            for (int i = 0; i < drs.Length; i++)
            {
                dt.Rows.Add(drs[i].ItemArray);
            }
            dt.AcceptChanges();
            return dt;
        }

        /// <summary>
        /// 外部数据绑定
        /// </summary>
        /// <param name="dict_code">字典编号</param>
        /// <param name="ctrl">要进行绑定的控件</param>
        /// <returns>数据源</returns>
        public static DataView DataBind(int dict_code, System.ComponentModel.IComponent ctrl)
        {
            return DataBindAll(dict_code, ctrl, false, DBNull.Value, "");
        }

        /// <summary>
        /// 外部数据绑定
        /// </summary>
        /// <param name="dict_code">字典编号</param>
        /// <param name="ctrl">要绑定的组件</param>
        /// <param name="isAll">是否要包含"全部"选项</param>
        /// <param name="sAllValue">"全部"选项值</param>
        /// <param name="sAllText">"全部"选项文本</param>
        /// <returns>数据源</returns>
        public static DataView DataBindAll(int dict_code, System.ComponentModel.IComponent ctrl, bool isAll, object sAllValue, string sAllText)
        {
            return DataBindAll(GetDictTable(dict_code).DefaultView, ctrl, isAll, sAllValue, sAllText);
        }

        /// <summary>
        /// 外部数据绑定
        /// </summary>
        /// <param name="source">外部数据源</param>
        /// <param name="ctrl">要进行绑定的控件</param>
        /// <returns>数据源</returns>
        public static DataView DataBind(DataView source, System.ComponentModel.IComponent ctrl)
        {
            return DataBindAll(source, ctrl, false, DBNull.Value, "");
        }

        /// <summary>
        /// 外部数据绑定
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="ctrl">要绑定的组件</param>
        /// <param name="isAll">是否要包含"全部"选项</param>
        /// <param name="sAllValue">"全部"选项值</param>
        /// <param name="sAllText">"全部"选项文本</param>
        /// <returns>数据源</returns>
        public static DataView DataBindAll(DataView source, System.ComponentModel.IComponent ctrl, bool isAll, object sAllValue, string sAllText)
        {

            String valueMember = source.Table.Columns[0].ColumnName;
            String displayMember = source.Table.Columns[(source.Table.Columns.Count < 2 ? 0 : 1)].ColumnName;
            if (isAll)
            {
                DataTable dt = source.ToTable();
                DataRow dr = dt.NewRow();
                dr[valueMember] = sAllValue;
                dr[displayMember] = sAllText;
                dt.Rows.InsertAt(dr, 0);
                source = dt.DefaultView;
            }
            if (ctrl is CheckedListBox)
            {
                CheckedListBox chklbox = ctrl as CheckedListBox;
                chklbox.Items.Clear();
                foreach (DataRowView drv in source)
                {
                    chklbox.Items.Add(new DictElement(drv[valueMember].ToString(), drv[displayMember].ToString(), drv));
                }
            }
            else if (ctrl is ListControl)
            {
                ListControl lstc = ctrl as ListControl;
                lstc.DataSource = null;
                lstc.ValueMember = valueMember;
                lstc.DisplayMember = displayMember;
                lstc.DataSource = source;
            }
            else if (ctrl is DataGridViewComboBoxColumn)
            {
                DataGridViewComboBoxColumn dgvcbc = (DataGridViewComboBoxColumn)ctrl;
                dgvcbc.DataSource = null;
                dgvcbc.ValueMember = valueMember;
                dgvcbc.DisplayMember = displayMember;
                dgvcbc.DataSource = source;
            }
            else if (ctrl is ToolStripComboBox)
            {
                ToolStripComboBox tscb = (ToolStripComboBox)ctrl;
                tscb.ComboBox.DataSource = null;
                tscb.ComboBox.ValueMember = valueMember;
                tscb.ComboBox.DisplayMember = displayMember;
                tscb.ComboBox.DataSource = source;
            }
            else if (ctrl is TextBox)
            {
                TextBox tb = (TextBox)ctrl;
                tb.DataBindings.Clear();
                tb.DataBindings.Add("Text", source, valueMember);
            }
            return source;
        }

    }
}
